<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="98px"
    >
      <el-form-item label="配送员姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入配送员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配送员手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入配送员手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="subStatus">
        <template>
          <el-select v-model="queryParams.subStatus" placeholder="请选择状态">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </template>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:deliveryman:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:deliveryman:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:deliveryman:remove']">删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:deliveryman:export']">导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deliverymanList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="配送员姓名" align="center" prop="name" />
      <el-table-column label="配送员手机号" align="center" prop="phone" />
      <el-table-column
        label="配送员住址"
        align="center"
        prop="address"
        show-overflow-tooltip
      />
      <el-table-column label="身份证正面" align="center" prop="cardUp">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cardUp" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="身份证反面" align="center" prop="cardDown">
        <template slot-scope="scope">
          <image-preview :src="scope.row.cardDown" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="subStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.subStatus == 0" effect="dark" type="warning"
            >待审核</el-tag
          >
          <el-tag
            v-else-if="scope.row.subStatus == 1"
            effect="dark"
            type="success"
            >通过</el-tag
          >
          <el-tag
            v-else-if="scope.row.subStatus == 2"
            effect="dark"
            type="danger"
            >拒绝</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.subStatus == 0"
            size="small"
            type="primary"
            @click="handleAudit(scope.row)"
            v-hasPermi="['business:deliveryman:edit']"
            >审核</el-button
          >
          <el-button
            v-if="scope.row.subStatus == 1"
            size="small"
            type="primary"
            @click="handleSet(scope.row)"
            v-hasPermi="['business:deliveryman:edit']"
            >配置区域</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:deliveryman:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 配置区域 -->
    <el-dialog
      title="配置区域"
      :visible.sync="openset"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="formSet" :model="formSet" label-width="110px">
        <el-form-item label="请选择区域">
          <el-cascader
            style="width: 100%"
            v-model="areaArr"
            :options="cityList"
            :props="props"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSet">确 定</el-button>
        <el-button @click="openset = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核配送员对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="openAudit"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核" prop="subStatus">
          <template>
            <el-select v-model="form.subStatus" placeholder="请选择状态">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </template>
        </el-form-item>
        <el-form-item
          label="拒绝原因"
          prop="auditDesc"
          v-if="form.subStatus == 2"
        >
          <el-input
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 10 }"
            placeholder="请输入拒绝原因"
            v-model="form.auditDesc"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDeliveryman,
  getDeliveryman,
  delDeliveryman,
  addDeliveryman,
  updateDeliveryman,
  cityListAll,
  addScope,
  listAllScope,
} from "@/api/business/deliveryman";
export default {
  name: "Deliveryman",
  data() {
    return {
      props: {
        multiple: true,
        label: "n",
        value: "v",
        children: "c",
      },
      cityList: [],
      formSet: {
        districtIds: "",
        senderId: "",
      },
      areaArr: [],
      openset: false,
      options: [
        { value: "0", label: "待审核" },
        { value: "1", label: "通过" },
        { value: "2", label: "拒绝" },
      ],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配送员表格数据
      deliverymanList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAudit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        address: null,
        cardUp: null,
        cardDown: null,
        subStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditDesc: [
          { required: true, message: "请输入拒绝原因", trigger: "blur" },
        ],
        subStatus: [
          { required: true, message: "状态：请选择状态", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getCity();
  },
  methods: {
    submitSet() {
      if (!this.areaArr.length) {
        this.$modal.msgWarning("请选择区域");
        return;
      }
      const arr = [];
      this.areaArr.forEach((item) => {
        arr.push(item[2]);
      });
      this.formSet.districtIds = arr.join(",");
      addScope(this.formSet).then((response) => {
        this.$modal.msgSuccess("配置成功");
        this.openset = false;
      });
    },
    getCity() {
      cityListAll().then((response) => {
        const arr = response.data;
        arr.forEach((a) => {
          a.c.forEach((b) => {
            b.c.forEach((c) => {
              c.c = null;
            });
          });
        });
        this.cityList = arr;
      });
    },
    /** 查询配送员列表 */
    getList() {
      this.loading = true;
      listDeliveryman(this.queryParams).then((response) => {
        this.deliverymanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        phone: null,
        address: null,
        cardUp: null,
        cardDown: null,
        subStatus: "0",
        auditDesc: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配送员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDeliveryman(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配送员";
      });
    },
    handleSet(row) {
      this.formSet = {
        districtIds: "",
        senderId: row.id,
      };
      this.areaArr = [];
      listAllScope({ senderId: row.id }).then((response) => {
        const arr = response.data;
        const newAarrr = [];
        arr.forEach((item) => {
          newAarrr.push(this.getParentsById(this.cityList, item.districtId));
        });
        this.areaArr = newAarrr;
        this.openset = true;
      });
    },
    getParentsById(list, id) {
      for (let i in list) {
        if (list[i].v == id) {
          //这里的list[i].id中的id根据你的需求可进行改变
          //查询到就返回该数组对象的value
          return [list[i].v];
        }
        if (list[i].c) {
          let node = this.getParentsById(list[i].c, id);
          if (node !== undefined) {
            //查询到把父节把父节点加到数组前面
            node.unshift(list[i].v);
            return node;
          }
        }
      }
    },
    handleAudit(row) {
      this.reset();
      const id = row.id || this.ids;
      getDeliveryman(id).then((response) => {
        this.form = response.data;
        this.openAudit = true;
        this.title = "审核配送员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDeliveryman(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.openAudit = false;
              this.getList();
            });
          } else {
            addDeliveryman(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除该配送员")
        .then(function () {
          return delDeliveryman(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/deliveryman/export",
        {
          ...this.queryParams,
        },
        `deliveryman_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
